﻿#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    m_pModel = new QStandardItemModel(20,5);//实例化 20行5列
    m_pModel->setHorizontalHeaderLabels(QStringList() << "Name" << "Class" << "Age" << "Sex" << "Grade");

    ui->tableView_1->setModel(m_pModel);//表格实例化
    ui->tableView_1->setAlternatingRowColors(true);//隔行变色
    ui->tableView_1->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//拉伸
    ui->tableView_1->horizontalHeader()->setStretchLastSection(true);//贴边

    ui->tableView_2->setModel(m_pModel);
    ui->tableView_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableView_2->horizontalHeader()->setStretchLastSection(true);

    connect(ui->btnClear , SIGNAL(clicked(bool)) , this , SLOT(onBtnClear()));//建立清除函数连接

    showMaximized();
}

Widget::~Widget()
{
    delete ui;
}

//列表清除
void Widget::onBtnClear()
{
    int rowCount = m_pModel->rowCount();

    for(int r = rowCount; r >= 0; --r)
    {
        m_pModel->removeRow(r);
    }

    for(int r = 0; r < rowCount; ++r)
    {
        m_pModel->insertRow(r);
    }

}
